<template>
  <!-- 整体人员结构 -->
  <div>
    <curd-title v-if="infoFlg" :title="'整体人员结构'" />
    <avue-crud
      :table-loading="staffLoading"
      :option="staffOption"
      :data="staffList"
      @on-load="onStaffLoad"
      @row-save="rowStaffSave"
      @row-update="rowStaffUpdate"
      @refresh-change="refreshStaffChange"
      @row-del="rowStaffDel"
      :row-class-name="delRowClassName"
    >
      <template slot="personnelType" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.personnelTypeFlag === '1'">{{ scope.row.personnelType }}</del>
          {{ scope.row.personnelTypeA }}
        </div>
        <span v-else>{{ scope.row.personnelType }}</span>
      </template>
      <template slot="totalPeople" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.totalPeopleFlag === '1'">{{ scope.row.totalPeople }}</del>
          {{ scope.row.totalPeopleA }}
        </div>
        <span v-else>{{ scope.row.totalPeople }}</span>
      </template>
      <template slot="avgWorkingYears" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.avgWorkingYearsFlag === '1'">{{ scope.row.avgWorkingYears }}</del>
          {{ scope.row.avgWorkingYearsA }}
        </div>
        <span v-else>{{ scope.row.avgWorkingYears }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { staffOption } from '@/option/tenant/supplier/contacts-info'
import CurdTitle from '@/components/base/title'
import { getStaff, addStaff, updateStaff, removeStaff } from '@/api/supplier/staff'
import { getStore } from '@/util/store'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'StaffInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      staffLoading: false,
      staffList: [],
      staffOption: staffOption.call(this)
    }
  },
  methods: {
    // ----整体人员结构----
    onStaffLoad() {
      this.infoFlg
        ? ((this.staffLoading = true),
          getStaff({ supplierId: this.supplierId }).then(res => {
            this.staffList = res.data.data
            this.staffLoading = false
          }))
        : 'false'
    },
    refreshStaffChange() {
      this.onStaffLoad()
    },
    rowStaffSave(row, done, loading) {
      addStaff({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onStaffLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowStaffUpdate(row, index, done, loading) {
      updateStaff(row).then(
        () => {
          this.onStaffLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowStaffDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeStaff(row.id)
        })
        .then(() => {
          this.onStaffLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
